let express = require('express');
let router = express.Router();

const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

// 实体类
const Response = require('../entity/http/Response');
const ObjId = require('../entity/common/ObjId');

// 业务层
const fileService = require('../service/fileService');

// 获取图片预签名URL接口
router.get('/getPublicImgUrl', async function (req, res, next) {
    // 读取请求参数
    let img = new ObjId(req.query, 'id');
    // 判断参数是否合法
    let flag = img.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 调用业务层
    fileService.getPublicImgUrl(img.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取图片预签名URL失败"));
    });
});

module.exports = router;
